package com.grass.cropTradingCenter.controller.user;

import com.grass.cropTradingCenter.common.Result;
import com.grass.cropTradingCenter.common.annotation.LoginFree;
import com.grass.cropTradingCenter.pojo.entity.MiniShop;
import com.grass.cropTradingCenter.pojo.entity.MiniUser;
import com.grass.cropTradingCenter.pojo.entity.ShippingAddress;
import com.grass.cropTradingCenter.pojo.vo.user.FollowOrUnFollowShop;
import com.grass.cropTradingCenter.pojo.vo.user.LoginVo;
import com.grass.cropTradingCenter.pojo.vo.user.RegisterVo;
import com.grass.cropTradingCenter.pojo.vo.user.SendUserLoginVo;
import com.grass.cropTradingCenter.service.MiniUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {
    @Autowired
    MiniUserService miniUserService;


    @LoginFree
    @PostMapping("/sendEmail/login")
    @ApiOperation("登录或找回密码邮箱验证码")
    public Result sendEmailLogin(@RequestBody SendUserLoginVo vo){
        miniUserService.sendEmailLogin(vo);
        return Result.success();
    }

    @LoginFree
    @PostMapping("/sendEmail/register")
    @ApiOperation("注册邮箱验证码")
    public Result sendEmailRegister(@RequestBody SendUserLoginVo vo){
        miniUserService.sendEmailRegister(vo);
        return Result.success();
    }

    @LoginFree
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<String> login(@RequestBody LoginVo vo){
        return Result.success(miniUserService.login(vo));
    }


    @LoginFree
    @PostMapping("/register")
    @ApiOperation("注册")
    public Result<String> register(@RequestBody @Validated RegisterVo vo){
        return Result.success(miniUserService.register(vo));
    }

    @LoginFree
    @PostMapping("/find/password")
    @ApiOperation("找回密码")
    public Result<String> findPassword(@RequestBody @Validated RegisterVo vo) throws InterruptedException {
        return Result.success(miniUserService.findPassword(vo));
    }

    @GetMapping("/userInfo")
    @ApiOperation("获取用户信息")
    public Result<MiniUser> getUserInfo() {
        return Result.success(miniUserService.getUserInfo());
    }

    @PostMapping("/modify")
    @ApiOperation("修改信息")
    public Result register(@RequestBody MiniUser user){
        miniUserService.modify(user);
        return Result.success();
    }

    @PostMapping("/add/address")
    @ApiOperation("新增或修改收货地址")
    public Result addOrUpdateAddress(@RequestBody ShippingAddress vo){
        miniUserService.addOrUpdateAddress(vo);
        return Result.success();
    }

    @GetMapping("/address")
    @ApiOperation("获取收货地址")
    public Result<List<ShippingAddress>> getAddress() {
        return Result.success(miniUserService.getAddress());
    }

    @GetMapping("/deleteAddress/{id}")
    @ApiOperation("删除收货地址")
    public Result deleteAddress(@PathVariable("id") int id) {
        miniUserService.deleteAddress(id);
        return Result.success();
    }

    @GetMapping("/getAddress/{id}")
    @ApiOperation("删除收货地址")
    public Result<ShippingAddress> getAddressById(@PathVariable("id") int id) {
        return Result.success(miniUserService.getAddressById(id));
    }

    @GetMapping("/getShopByShopId/{id}")
    @ApiOperation("获取店铺信息")
    public Result<MiniShop> getShopByShopId(@PathVariable("id") int id) {
        return Result.success(miniUserService.getShopByShopId(id));
    }

    @PostMapping("/followOrUnFollowShop")
    @ApiOperation("收藏店铺")
    public Result followOrUnFollowShop(@RequestBody FollowOrUnFollowShop vo) {
        miniUserService.followOrUnFollowShop(vo);
        return Result.success();
    }

    @GetMapping("/startShopList")
    @ApiOperation("收藏店铺列表")
    public Result<List<MiniShop>> startShopList() {
        return Result.success(miniUserService.startShopList());
    }

}
